package com.kyubi.common.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class BigramModel implements IModel{
	
	public BigramModel(){
		m_BigramCounts = new HashMap<String, Double>();
	}
	
	public void read(Vector<String> data){
		collectBigrams(data);
	}
	public Map<String, Double> getModelCounts(){
		return m_BigramCounts;
	}
	
	private void collectBigrams(Vector<String> data){
		for(int i = 0; i < data.size(); ++i){
			String sentence = data.get(i); 
			String[] words = sentence.split("[ ]+");
			for(int j = 0; j < words.length - 1; j++) {
				String bigram = words[j] + "_" + words[j+1];
				//collecting the word bigrams....
				if(m_BigramCounts.containsKey(bigram)){
					m_BigramCounts.put(bigram, m_BigramCounts.get(bigram) + 1);
				}
				else{
					m_BigramCounts.put(bigram, new Double(1));
				}
				//collecting the word unigrams...
				if(m_BigramCounts.containsKey(words[j])){
					m_BigramCounts.put(words[j], m_BigramCounts.get(words[j]) + 1);
				}
				else{
					m_BigramCounts.put(words[j], new Double(1));
				}
			}
			
		}
	}
	
	
	public Map<String, Double> m_BigramCounts; //contains both word bigrams and unigrams	
}
